﻿using System.Reflection;
using System.Collections.Generic;

namespace MappingFactory.NHUtils
{
    public class NHBusinessBuilder
    {
        NHParams m_MappingParams;

        /// <summary>
        /// 库表构建类
        /// </summary>
        NHBuilder m_Builder;

        /// <summary>
        /// 无参构造
        /// </summary>
        /// <param name="mappingAssemblyFile"></param>
        /// <param name="outMappingFilePath"></param>
        public NHBusinessBuilder(string mappingAssemblyFile, string outMappingFilePath)
        {
            BuildingMappingParams(mappingAssemblyFile, outMappingFilePath);
            m_Builder = new NHBuilder(m_MappingParams);

            if (!string.IsNullOrEmpty(m_MappingParams.MappingAssemblyFile))
            {
                Assembly ass = Assembly.LoadFile(m_MappingParams.MappingAssemblyFile);
                BuildMappingsFromAssembly(ass);
            }
        }

        /// <summary>
        /// 无参构造
        /// </summary>
        /// <param name="mappingAssemblyFile"></param>
        /// <param name="outMappingFilePath"></param>
        public NHBusinessBuilder(string mappingAssemblyFile, string outMappingFilePath,List<MapperItem> MapperItems)
        {
            BuildingMappingParams(mappingAssemblyFile, outMappingFilePath);
            m_Builder = new NHBuilder(m_MappingParams);

            if (!string.IsNullOrEmpty(m_MappingParams.MappingAssemblyFile))
            {
                Assembly ass = Assembly.LoadFile(m_MappingParams.MappingAssemblyFile);
                BuildMappingsFromAssembly(ass, MapperItems);
            }
        }

        /// <summary>
        /// 构造库表结构所需基本参数配置
        /// </summary>
        /// <param name="mappingAssemblyFile"></param>
        /// <param name="outMappingFilePath"></param>
        private void BuildingMappingParams(string mappingAssemblyFile, string outMappingFilePath)
        {
            m_MappingParams = new NHParams();
            m_MappingParams.OutMappingFilePath = outMappingFilePath;
            m_MappingParams.MappingAssemblyFile = mappingAssemblyFile;
            m_MappingParams.Suffix = ".hbm.xml";
        }

        /// <summary>
        /// 从程序集中加载映射
        /// </summary>
        /// <param name="assembly"></param>
        public void BuildMappingsFromAssembly(Assembly assembly)
        {
            m_Builder.AddMappingsFromAssembly(assembly);
        }

        /// <summary>
        /// 从程序集中加载映射
        /// </summary>
        /// <param name="assembly"></param>
        public void BuildMappingsFromAssembly(Assembly assembly, List<MapperItem> MapperItems)
        {
            m_Builder.AddMappingsFromAssembly(assembly, MapperItems);
        }

        /// <summary>
        /// 生成xml配置文件
        /// </summary>
        public void BuildMappingFile()
        {
            m_Builder.BuildingMappingFile();
        }

        /// <summary>
        /// 构造库表结构
        /// </summary>
        public void BuildDBSchema()
        {
            m_Builder.BuildingDBSchema(true);
        }
    }
}
